package vip.specimen.system.service;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.specimen.system.datasource.entity.SystemBlogrollEntity;
import vip.specimen.system.datasource.entity.SystemUserEntity;
import vip.specimen.system.repository.SystemBlogrollRepository;
import vip.specimen.system.datasource.validation.SystemBlogrollValidation;
import vip.specimen.system.utils.CommonUtil;
import vip.specimen.system.utils.DataBaseUtil;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Set;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/3/5 9:18
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Service
@Slf4j
public class SystemBlogrollService {
    @Resource
    private SystemBlogrollRepository blogrollRepository;
    @Resource
    private SystemUserService systemUserService;
    @Resource
    private HttpServletRequest request;

    /**
     * 新增友情链接
     *
     * @param blogroll_v 验证对象
     * @return
     */
    @Transactional
    public SystemBlogrollEntity insert(SystemBlogrollValidation blogroll_v) {
        SystemUserEntity current_user = (SystemUserEntity) SecurityUtils.getSubject().getPrincipal();
        return this.blogrollRepository.saveAndFlush(new SystemBlogrollEntity(blogroll_v.getLinkName(), blogroll_v.getLinkUrl(), this.systemUserService.findByUuid(current_user.getUuid()), Boolean.FALSE, blogroll_v.getEndTime(), blogroll_v.getColor()));
    }

    /**
     * 获取全部数据
     *
     * @return 数据集
     */
    public Page<SystemBlogrollEntity> blogrollList() {
        JSONObject param = CommonUtil.pageParams(this.request);
        return this.blogrollRepository.findAll(DataBaseUtil.getPageable(param.getIntValue("page"), param.getIntValue("pageSize"), DataBaseUtil.getSort("desc", "createTime")));
    }

    /**
     * 根据UUID删除数据
     *
     * @param uuid UUID
     * @return 成功 or 失败
     */
    @Transactional
    public boolean delete(UUID uuid) {
        try {
            if (this.blogrollRepository.existsById(uuid)) {
                this.blogrollRepository.deleteById(uuid);
                return true;
            }
        } catch (Exception e) {
            log.error("删除API异常:" + e.getMessage());
        }
        return false;
    }

    /**
     * 根据UUID更新数据
     *
     * @param uuid       UUID
     * @param blogroll_v 验证类
     * @return 数据
     */
    @Transactional
    public SystemBlogrollEntity update(UUID uuid, SystemBlogrollValidation blogroll_v) {
        try {
            if (this.blogrollRepository.existsById(uuid)) {
                SystemBlogrollEntity blogroll = this.blogrollRepository.findById(uuid).get();
                blogroll.setColor(blogroll_v.getColor());
                blogroll.setEndTime(blogroll_v.getEndTime());
                blogroll.setLinkName(blogroll_v.getLinkName());
                blogroll.setLinkUrl(blogroll_v.getLinkUrl());
                blogroll.setUpdateTime(Calendar.getInstance().getTime());
                this.blogrollRepository.save(blogroll);
                return blogroll;
            }
        } catch (Exception e) {
            log.error("更新友情链接异常:" + e.getMessage());
        }
        return null;
    }

    /**
     * 根据uuid 更新链接颜色
     *
     * @param uuid  uuid
     * @param color 颜色 16进制
     * @return o
     */
    @Transactional
    public SystemBlogrollEntity update(UUID uuid, String color) {
        try {
            if (this.blogrollRepository.existsById(uuid)) {
                SystemBlogrollEntity blogroll = this.blogrollRepository.findById(uuid).get();
                blogroll.setColor(color);
                blogroll.setUpdateTime(Calendar.getInstance().getTime());
                this.blogrollRepository.save(blogroll);
                return blogroll;
            }
        } catch (Exception e) {
            log.error("更新友情链接异常:" + e.getMessage());
        }
        return null;
    }

    /**
     * 根据uuid更新友情链接状态
     *
     * @param uuid uuid
     * @return 数据
     */
    @Transactional
    public SystemBlogrollEntity update(UUID uuid) {
        try {
            if (this.blogrollRepository.existsById(uuid)) {
                SystemBlogrollEntity blogroll = this.blogrollRepository.findById(uuid).get();
                blogroll.setEnable(!blogroll.isEnable());
                blogroll.setUpdateTime(Calendar.getInstance().getTime());
                this.blogrollRepository.save(blogroll);
                return blogroll;
            }
        } catch (Exception e) {
            log.error("更新友情链接异常:" + e.getMessage());
        }
        return null;
    }

    /**
     * 根据UUID批量删除
     *
     * @param uuids UUID集
     * @return 成功数
     */
    @Transactional
    public int delete(Set<UUID> uuids) {
        int successNum = 0;
        for (UUID uuid : uuids) {
            successNum += this.delete(uuid) ? 1 : 0;
        }
        return successNum;
    }

    /**
     * 统计
     *
     * @return 数目
     */
    public long count() {
        return this.blogrollRepository.count();
    }
}
